<template><div><h2 id="sphinx-音译-斯芬克斯" tabindex="-1"><a class="header-anchor" href="#sphinx-音译-斯芬克斯"><span>Sphinx（音译：斯芬克斯）</span></a></h2>
<p>Yii 2 官方 <code v-pre>yii2-sphinx</code> 扩展为框架赋予了 <a href="http://sphinxsearch.com/docs" target="_blank" rel="noopener noreferrer">Sphinx</a> 全文搜索引擎扩展，它支持包括 <a href="http://sphinxsearch.com/docs/current.html#rt-indexes" target="_blank" rel="noopener noreferrer">实时索引（Real-time Indexes）</a> 在内的所有 Sphinx 特性。</p>
<h2 id="安装" tabindex="-1"><a class="header-anchor" href="#安装"><span>安装</span></a></h2>
<p>Sphinx 一般是配合 mysql 一起使用。Sphinx 服务端 <a href="https://sphinxsearch.com/downloads/" title="安装" target="_blank" rel="noopener noreferrer">下载安装</a> 版本，最低要求 &gt;= 2.0，然而，为了使用所有扩展特性，Sphinx 版本要求 2.2 或更高。</p>
<p>安装此扩展的首选方式是通过 <a href="http://getcomposer.org/download/" target="_blank" rel="noopener noreferrer">composer</a>，命令如下：</p>
<div class="language-bash line-numbers-mode" data-highlighter="prismjs" data-ext="sh" data-title="sh"><pre v-pre class="language-bash"><code><span class="line"><span class="token function">composer</span> require yiisoft/yii2-sphinx</span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><p>或者在您的 <code v-pre>composer.json</code> 文件中的 require 部分增加以下内容：</p>
<div class="language-json line-numbers-mode" data-highlighter="prismjs" data-ext="json" data-title="json"><pre v-pre class="language-json"><code><span class="line"><span class="token property">"yiisoft/yii2-sphinx"</span><span class="token operator">:</span> <span class="token string">"~2.0.0"</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><p>然后执行 <code v-pre>composer install</code> 命令即可。</p>
<h2 id="配置" tabindex="-1"><a class="header-anchor" href="#配置"><span>配置</span></a></h2>
<p>这个扩展使用 MySQL 协议和 <a href="http://sphinxsearch.com/docs/current.html#sphinxql" target="_blank" rel="noopener noreferrer">SphinxQL（SQL 的一种方言）</a>查询语言与 Sphinx 搜索守护进程交互。为了设置 Sphinx “searchd” 支持 MySQL 协议，您需要添加以下配置：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line">searchd</span>
<span class="line"><span class="token punctuation">{</span></span>
<span class="line">    listen <span class="token operator">=</span> localhost<span class="token punctuation">:</span><span class="token number">9306</span><span class="token punctuation">:</span>mysql41</span>
<span class="line">    <span class="token operator">...</span></span>
<span class="line"><span class="token punctuation">}</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>要使用这个扩展，只需在您的应用程序配置中添加以下代码：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token keyword">return</span> <span class="token punctuation">[</span></span>
<span class="line">    <span class="token comment">//....</span></span>
<span class="line">    <span class="token string single-quoted-string">'components'</span> <span class="token operator">=></span> <span class="token punctuation">[</span></span>
<span class="line">        <span class="token string single-quoted-string">'sphinx'</span> <span class="token operator">=></span> <span class="token punctuation">[</span></span>
<span class="line">            <span class="token string single-quoted-string">'class'</span> <span class="token operator">=></span> <span class="token string single-quoted-string">'yii\sphinx\Connection'</span><span class="token punctuation">,</span></span>
<span class="line">            <span class="token string single-quoted-string">'dsn'</span> <span class="token operator">=></span> <span class="token string single-quoted-string">'mysql:host=127.0.0.1;port=9306;'</span><span class="token punctuation">,</span></span>
<span class="line">            <span class="token string single-quoted-string">'username'</span> <span class="token operator">=></span> <span class="token string single-quoted-string">''</span><span class="token punctuation">,</span></span>
<span class="line">            <span class="token string single-quoted-string">'password'</span> <span class="token operator">=></span> <span class="token string single-quoted-string">''</span><span class="token punctuation">,</span></span>
<span class="line">        <span class="token punctuation">]</span><span class="token punctuation">,</span></span>
<span class="line">    <span class="token punctuation">]</span><span class="token punctuation">,</span></span>
<span class="line"><span class="token punctuation">]</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h2 id="基本用法" tabindex="-1"><a class="header-anchor" href="#基本用法"><span>基本用法</span></a></h2>
<p>由于这个扩展使用 MySQL 协议访问 Sphinx，它从常规的“yii\db”包里共享基本方法和很多代码。运行 SphinxQL 查询非常类似于常规的 SQL 查询：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token variable">$sql</span> <span class="token operator">=</span> <span class="token string single-quoted-string">'SELECT * FROM idx_item WHERE group_id = :group_id'</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token variable">$params</span> <span class="token operator">=</span> <span class="token punctuation">[</span></span>
<span class="line">    <span class="token string single-quoted-string">'group_id'</span> <span class="token operator">=></span> <span class="token number">17</span></span>
<span class="line"><span class="token punctuation">]</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token variable">$rows</span> <span class="token operator">=</span> <span class="token class-name static-context">Yii</span><span class="token operator">::</span><span class="token variable">$app</span><span class="token operator">-></span><span class="token property">sphinx</span><span class="token operator">-></span><span class="token function">createCommand</span><span class="token punctuation">(</span><span class="token variable">$sql</span><span class="token punctuation">,</span> <span class="token variable">$params</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">queryAll</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>您还可以使用查询生成器：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token keyword">use</span> <span class="token package">yii<span class="token punctuation">\</span>sphinx<span class="token punctuation">\</span>Query</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token variable">$query</span> <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">Query</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token variable">$rows</span> <span class="token operator">=</span> <span class="token variable">$query</span><span class="token operator">-></span><span class="token function">select</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'id, price'</span><span class="token punctuation">)</span></span>
<span class="line">    <span class="token operator">-></span><span class="token function">from</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'idx_item'</span><span class="token punctuation">)</span></span>
<span class="line">    <span class="token operator">-></span><span class="token function">andWhere</span><span class="token punctuation">(</span><span class="token punctuation">[</span><span class="token string single-quoted-string">'group_id'</span> <span class="token operator">=></span> <span class="token number">1</span><span class="token punctuation">]</span><span class="token punctuation">)</span></span>
<span class="line">    <span class="token operator">-></span><span class="token function">all</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><blockquote>
<p>Note：默认情况下，Sphinx将任何查询返回的记录数量限制为 10 条。如果您需要获得更多的记录，您应该明确地指定 limit 的值。</p>
</blockquote>
<h2 id="组装-match-语句" tabindex="-1"><a class="header-anchor" href="#组装-match-语句"><span>组装 MATCH 语句</span></a></h2>
<p>Sphinx 用法的意义在于它的全文搜索功能，在 SphinxQL 中，它是通过 ‘MATCH’ 语句提供的。你总是可以手动组合它作为“where”条件的一部分，但如果你使用的是 <code v-pre>yii\sphinx\Query</code>，你可以通过 <code v-pre>yii\sphinx\Query::match()</code> 来实现，示例如下：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token keyword">use</span> <span class="token package">yii<span class="token punctuation">\</span>sphinx<span class="token punctuation">\</span>Query</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token variable">$query</span> <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">Query</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token variable">$rows</span> <span class="token operator">=</span> <span class="token variable">$query</span><span class="token operator">-></span><span class="token function">from</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'idx_item'</span><span class="token punctuation">)</span></span>
<span class="line">    <span class="token operator">-></span><span class="token function">match</span><span class="token punctuation">(</span><span class="token variable">$_POST</span><span class="token punctuation">[</span><span class="token string single-quoted-string">'search'</span><span class="token punctuation">]</span><span class="token punctuation">)</span></span>
<span class="line">    <span class="token operator">-></span><span class="token function">all</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>请注意，Sphinx ‘MATCH’ 语句参数，使用复杂的内部语法来进行更好的调优。默认情况下，<code v-pre>yii\sphinx\Query::match()</code> 将转义与该语法相关的所有特殊字符。因此，如果你想使用复杂的 ‘MATCH’ 语句，你应该使用 <code v-pre>yii\db\Expression</code>，例如：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token keyword">use</span> <span class="token package">yii<span class="token punctuation">\</span>sphinx<span class="token punctuation">\</span>Query</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token keyword">use</span> <span class="token package">yii<span class="token punctuation">\</span>db<span class="token punctuation">\</span>Expression</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token variable">$query</span> <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">Query</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token variable">$rows</span> <span class="token operator">=</span> <span class="token variable">$query</span><span class="token operator">-></span><span class="token function">from</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'idx_item'</span><span class="token punctuation">)</span></span>
<span class="line">    <span class="token operator">-></span><span class="token function">match</span><span class="token punctuation">(</span><span class="token keyword">new</span> <span class="token class-name">Expression</span><span class="token punctuation">(</span><span class="token string single-quoted-string">':match'</span><span class="token punctuation">,</span> <span class="token punctuation">[</span><span class="token string single-quoted-string">'match'</span> <span class="token operator">=></span> <span class="token string single-quoted-string">'@(content) '</span> <span class="token operator">.</span> <span class="token class-name static-context">Yii</span><span class="token operator">::</span><span class="token variable">$app</span><span class="token operator">-></span><span class="token property">sphinx</span><span class="token operator">-></span><span class="token function">escapeMatchValue</span><span class="token punctuation">(</span><span class="token variable">$_POST</span><span class="token punctuation">[</span><span class="token string single-quoted-string">'search'</span><span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">)</span></span>
<span class="line">    <span class="token operator">-></span><span class="token function">all</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><blockquote>
<p>Note：如果你构建 MATCH 参数，请确保使用 <code v-pre>\yii\sphinx\Connection::escapeMatchValue()</code> 来正确转义任何特殊字符，这可能会中断查询。</p>
</blockquote>
<p>从 2.0.6 版本开始，你可以使用 <code v-pre>\yii\sphinx\MatchExpression</code> 来针对 ‘MATCH’ 语句组合。它允许 ‘MATCH’ 表达式组合，以类似的方式使用占位符作为绑定参数，那些值会通过使用 <code v-pre>\yii\sphinx\Connection::escapeMatchValue()</code> 自动转义。例如：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token keyword">use</span> <span class="token package">yii<span class="token punctuation">\</span>sphinx<span class="token punctuation">\</span>Query</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token keyword">use</span> <span class="token package">yii<span class="token punctuation">\</span>sphinx<span class="token punctuation">\</span>MatchExpression</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token variable">$rows</span> <span class="token operator">=</span> <span class="token punctuation">(</span><span class="token keyword">new</span> <span class="token class-name">Query</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span></span>
<span class="line">    <span class="token operator">-></span><span class="token function">match</span><span class="token punctuation">(</span><span class="token keyword">new</span> <span class="token class-name">MatchExpression</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'@title :title'</span><span class="token punctuation">,</span> <span class="token punctuation">[</span><span class="token string single-quoted-string">'title'</span> <span class="token operator">=></span> <span class="token string single-quoted-string">'Yii'</span><span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">)</span> <span class="token comment">// value of ':title' will be escaped automatically</span></span>
<span class="line">    <span class="token operator">-></span><span class="token function">all</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h2 id="使用活动记录" tabindex="-1"><a class="header-anchor" href="#使用活动记录"><span>使用活动记录</span></a></h2>
<p>这个扩展提供的活动记录解决方案与 <code v-pre>\yii\db\ActiveRecord</code> 类似。要声明一个活动记录类，你需要继承 <code v-pre>\yii\sphinx\ActiveRecord</code> 并实现 <code v-pre>indexName</code> 方法：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token keyword">use</span> <span class="token package">yii<span class="token punctuation">\</span>sphinx<span class="token punctuation">\</span>ActiveRecord</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token keyword">class</span> <span class="token class-name-definition class-name">Article</span> <span class="token keyword">extends</span> <span class="token class-name">ActiveRecord</span></span>
<span class="line"><span class="token punctuation">{</span></span>
<span class="line">    <span class="token doc-comment comment">/**</span>
<span class="line">     * <span class="token keyword">@return</span> <span class="token class-name"><span class="token keyword">string</span></span> the name of the index associated with this ActiveRecord class.</span>
<span class="line">     */</span></span>
<span class="line">    <span class="token keyword">public</span> <span class="token keyword">static</span> <span class="token keyword">function</span> <span class="token function-definition function">indexName</span><span class="token punctuation">(</span><span class="token punctuation">)</span></span>
<span class="line">    <span class="token punctuation">{</span></span>
<span class="line">        <span class="token keyword">return</span> <span class="token string single-quoted-string">'idx_article'</span><span class="token punctuation">;</span></span>
<span class="line">    <span class="token punctuation">}</span></span>
<span class="line"><span class="token punctuation">}</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>您可以使用 [[match()]，[[andMatch()]] 和 [[orMatch()] 来组合多个条件。每个条件都可以使用数组语法来指定，类似于使用 <code v-pre>\yii\sphinx\Query:where]]</code>，例如：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token keyword">use</span> <span class="token package">yii<span class="token punctuation">\</span>sphinx<span class="token punctuation">\</span>Query</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token keyword">use</span> <span class="token package">yii<span class="token punctuation">\</span>sphinx<span class="token punctuation">\</span>MatchExpression</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token variable">$rows</span> <span class="token operator">=</span> <span class="token punctuation">(</span><span class="token keyword">new</span> <span class="token class-name">Query</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span></span>
<span class="line">    <span class="token operator">-></span><span class="token function">match</span><span class="token punctuation">(</span></span>
<span class="line">        <span class="token comment">// produces '((@title "Yii") (@author "Paul")) | (@content "Sphinx")' :</span></span>
<span class="line">        <span class="token punctuation">(</span><span class="token keyword">new</span> <span class="token class-name">MatchExpression</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span></span>
<span class="line">            <span class="token operator">-></span><span class="token function">match</span><span class="token punctuation">(</span><span class="token punctuation">[</span><span class="token string single-quoted-string">'title'</span> <span class="token operator">=></span> <span class="token string single-quoted-string">'Yii'</span><span class="token punctuation">]</span><span class="token punctuation">)</span></span>
<span class="line">            <span class="token operator">-></span><span class="token function">andMatch</span><span class="token punctuation">(</span><span class="token punctuation">[</span><span class="token string single-quoted-string">'author'</span> <span class="token operator">=></span> <span class="token string single-quoted-string">'Paul'</span><span class="token punctuation">]</span><span class="token punctuation">)</span></span>
<span class="line">            <span class="token operator">-></span><span class="token function">orMatch</span><span class="token punctuation">(</span><span class="token punctuation">[</span><span class="token string single-quoted-string">'content'</span> <span class="token operator">=></span> <span class="token string single-quoted-string">'Sphinx'</span><span class="token punctuation">]</span><span class="token punctuation">)</span></span>
<span class="line">    <span class="token punctuation">)</span></span>
<span class="line">    <span class="token operator">-></span><span class="token function">all</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>你也可以用“MAYBE”、“PROXIMITY”等特殊操作符组合表达式。例如：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token keyword">use</span> <span class="token package">yii<span class="token punctuation">\</span>sphinx<span class="token punctuation">\</span>Query</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token keyword">use</span> <span class="token package">yii<span class="token punctuation">\</span>sphinx<span class="token punctuation">\</span>MatchExpression</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token variable">$rows</span> <span class="token operator">=</span> <span class="token punctuation">(</span><span class="token keyword">new</span> <span class="token class-name">Query</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span></span>
<span class="line">    <span class="token operator">-></span><span class="token function">match</span><span class="token punctuation">(</span></span>
<span class="line">        <span class="token comment">// produces '@title "Yii" MAYBE "Sphinx"' :</span></span>
<span class="line">        <span class="token punctuation">(</span><span class="token keyword">new</span> <span class="token class-name">MatchExpression</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">match</span><span class="token punctuation">(</span><span class="token punctuation">[</span></span>
<span class="line">            <span class="token string single-quoted-string">'maybe'</span><span class="token punctuation">,</span></span>
<span class="line">            <span class="token string single-quoted-string">'title'</span><span class="token punctuation">,</span></span>
<span class="line">            <span class="token string single-quoted-string">'Yii'</span><span class="token punctuation">,</span></span>
<span class="line">            <span class="token string single-quoted-string">'Sphinx'</span><span class="token punctuation">,</span></span>
<span class="line">        <span class="token punctuation">]</span><span class="token punctuation">)</span></span>
<span class="line">    <span class="token punctuation">)</span></span>
<span class="line">    <span class="token operator">-></span><span class="token function">all</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token variable">$rows</span> <span class="token operator">=</span> <span class="token punctuation">(</span><span class="token keyword">new</span> <span class="token class-name">Query</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span></span>
<span class="line">    <span class="token operator">-></span><span class="token function">match</span><span class="token punctuation">(</span></span>
<span class="line">        <span class="token comment">// produces '@title "Yii"~10' :</span></span>
<span class="line">        <span class="token punctuation">(</span><span class="token keyword">new</span> <span class="token class-name">MatchExpression</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">match</span><span class="token punctuation">(</span><span class="token punctuation">[</span></span>
<span class="line">            <span class="token string single-quoted-string">'proximity'</span><span class="token punctuation">,</span></span>
<span class="line">            <span class="token string single-quoted-string">'title'</span><span class="token punctuation">,</span></span>
<span class="line">            <span class="token string single-quoted-string">'Yii'</span><span class="token punctuation">,</span></span>
<span class="line">            <span class="token number">10</span><span class="token punctuation">,</span></span>
<span class="line">        <span class="token punctuation">]</span><span class="token punctuation">)</span></span>
<span class="line">    <span class="token punctuation">)</span></span>
<span class="line">    <span class="token operator">-></span><span class="token function">all</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h2 id="获取查询-meta-信息" tabindex="-1"><a class="header-anchor" href="#获取查询-meta-信息"><span>获取查询 META 信息</span></a></h2>
<p>Sphinx allows fetching statistical information about last performed query via <a href="http://sphinxsearch.com/docs/current.html#sphinxql-show-meta" target="_blank" rel="noopener noreferrer">SHOW META</a> SphinxQL statement. This information is commonly used to get total count of rows in the index without extra <code v-pre>SELECT COUNT(*) ...</code> query. Although you can always run such query manually, <code v-pre>yii\sphinx\Query</code> allows you to do this automatically without extra efforts. All you need to do is enable <code v-pre>yii\sphinx\Query::showMeta</code> and use <code v-pre>yii\sphinx\Query::search()</code> to fetch all rows and meta information:</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token variable">$query</span> <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">Query</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token variable">$results</span> <span class="token operator">=</span> <span class="token variable">$query</span><span class="token operator">-></span><span class="token function">from</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'idx_item'</span><span class="token punctuation">)</span></span>
<span class="line">    <span class="token operator">-></span><span class="token function">match</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'foo'</span><span class="token punctuation">)</span></span>
<span class="line">    <span class="token operator">-></span><span class="token function">showMeta</span><span class="token punctuation">(</span><span class="token constant boolean">true</span><span class="token punctuation">)</span> <span class="token comment">// enable automatic 'SHOW META' query</span></span>
<span class="line">    <span class="token operator">-></span><span class="token function">search</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment">// retrieve all rows and META information</span></span>
<span class="line"></span>
<span class="line"><span class="token variable">$items</span> <span class="token operator">=</span> <span class="token variable">$results</span><span class="token punctuation">[</span><span class="token string single-quoted-string">'hits'</span><span class="token punctuation">]</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token variable">$meta</span> <span class="token operator">=</span> <span class="token variable">$results</span><span class="token punctuation">[</span><span class="token string single-quoted-string">'meta'</span><span class="token punctuation">]</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token variable">$totalItemCount</span> <span class="token operator">=</span> <span class="token variable">$results</span><span class="token punctuation">[</span><span class="token string single-quoted-string">'meta'</span><span class="token punctuation">]</span><span class="token punctuation">[</span><span class="token string single-quoted-string">'total'</span><span class="token punctuation">]</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><blockquote>
<p>Note: Total item count that can be extracted from ‘meta’ is limited to <code v-pre>max_matches</code> sphinx option. If your index contains more records than <code v-pre>max_matches</code> value (usually - 1000), you should either raise up <code v-pre>max_matches</code> via [[Query::options]] or use [[Query::count()]] to retrieve records count.</p>
</blockquote>
<h2 id="facet-子句" tabindex="-1"><a class="header-anchor" href="#facet-子句"><span>FACET 子句</span></a></h2>
<p>Since version 2.2.3 Sphinx provides ability of the facet searching via <code v-pre>FACET</code> clause:</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token constant">SELECT</span> <span class="token operator">*</span> <span class="token constant">FROM</span> idx_item <span class="token constant">FACET</span> brand_id <span class="token constant">FACET</span> categories<span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><p><code v-pre>yii\sphinx\Query</code> supports composition of this clause as well as fetching facet results. You may specify facets via <code v-pre>yii\sphinx\Query::facets</code>. In order to fetch results with facets you need to use <code v-pre>yii\sphinx\Query::search()</code> method. For example:</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token keyword">use</span> <span class="token package">yii<span class="token punctuation">\</span>sphinx<span class="token punctuation">\</span>Query</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token variable">$query</span> <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">Query</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token variable">$results</span> <span class="token operator">=</span> <span class="token variable">$query</span><span class="token operator">-></span><span class="token function">from</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'idx_item'</span><span class="token punctuation">)</span></span>
<span class="line">    <span class="token operator">-></span><span class="token function">facets</span><span class="token punctuation">(</span><span class="token punctuation">[</span></span>
<span class="line">        <span class="token string single-quoted-string">'brand_id'</span><span class="token punctuation">,</span></span>
<span class="line">        <span class="token string single-quoted-string">'categories'</span><span class="token punctuation">,</span></span>
<span class="line">    <span class="token punctuation">]</span><span class="token punctuation">)</span></span>
<span class="line">    <span class="token operator">-></span><span class="token function">search</span><span class="token punctuation">(</span><span class="token variable">$connection</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment">// retrieve all rows and facets</span></span>
<span class="line"></span>
<span class="line"><span class="token variable">$items</span> <span class="token operator">=</span> <span class="token variable">$results</span><span class="token punctuation">[</span><span class="token string single-quoted-string">'hits'</span><span class="token punctuation">]</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token variable">$facets</span> <span class="token operator">=</span> <span class="token variable">$results</span><span class="token punctuation">[</span><span class="token string single-quoted-string">'facets'</span><span class="token punctuation">]</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token keyword">foreach</span> <span class="token punctuation">(</span><span class="token variable">$results</span><span class="token punctuation">[</span><span class="token string single-quoted-string">'facets'</span><span class="token punctuation">]</span><span class="token punctuation">[</span><span class="token string single-quoted-string">'brand_id'</span><span class="token punctuation">]</span> <span class="token keyword">as</span> <span class="token variable">$frame</span><span class="token punctuation">)</span> <span class="token punctuation">{</span></span>
<span class="line">    <span class="token variable">$brandId</span> <span class="token operator">=</span> <span class="token variable">$frame</span><span class="token punctuation">[</span><span class="token string single-quoted-string">'value'</span><span class="token punctuation">]</span><span class="token punctuation">;</span></span>
<span class="line">    <span class="token variable">$count</span> <span class="token operator">=</span> <span class="token variable">$frame</span><span class="token punctuation">[</span><span class="token string single-quoted-string">'count'</span><span class="token punctuation">]</span><span class="token punctuation">;</span></span>
<span class="line">    <span class="token operator">...</span></span>
<span class="line"><span class="token punctuation">}</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><blockquote>
<p>Note: make sure you are using Sphinx server version 2.2.3 or higher before attempting to use facet feature.</p>
</blockquote>
<p>You may specify additional facet options like <code v-pre>select</code> or <code v-pre>order</code> using an array format:</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token keyword">use</span> <span class="token package">yii<span class="token punctuation">\</span>db<span class="token punctuation">\</span>Expression</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token keyword">use</span> <span class="token package">yii<span class="token punctuation">\</span>sphinx<span class="token punctuation">\</span>Query</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token variable">$query</span> <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">Query</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token variable">$results</span> <span class="token operator">=</span> <span class="token variable">$query</span><span class="token operator">-></span><span class="token function">from</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'idx_item'</span><span class="token punctuation">)</span></span>
<span class="line">    <span class="token operator">-></span><span class="token function">facets</span><span class="token punctuation">(</span><span class="token punctuation">[</span></span>
<span class="line">        <span class="token string single-quoted-string">'price'</span> <span class="token operator">=></span> <span class="token punctuation">[</span></span>
<span class="line">            <span class="token string single-quoted-string">'select'</span> <span class="token operator">=></span> <span class="token string single-quoted-string">'INTERVAL(price,200,400,600,800) AS price'</span><span class="token punctuation">,</span> <span class="token comment">// using function</span></span>
<span class="line">            <span class="token string single-quoted-string">'order'</span> <span class="token operator">=></span> <span class="token punctuation">[</span><span class="token string single-quoted-string">'FACET()'</span> <span class="token operator">=></span> <span class="token constant">SORT_ASC</span><span class="token punctuation">]</span><span class="token punctuation">,</span></span>
<span class="line">        <span class="token punctuation">]</span><span class="token punctuation">,</span></span>
<span class="line">        <span class="token string single-quoted-string">'name_in_json'</span> <span class="token operator">=></span> <span class="token punctuation">[</span></span>
<span class="line">            <span class="token string single-quoted-string">'select'</span> <span class="token operator">=></span> <span class="token punctuation">[</span><span class="token keyword">new</span> <span class="token class-name">Expression</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'json_attr.name AS name_in_json'</span><span class="token punctuation">)</span><span class="token punctuation">]</span><span class="token punctuation">,</span> <span class="token comment">// have to use `Expression` to avoid unnecessary quoting</span></span>
<span class="line">        <span class="token punctuation">]</span><span class="token punctuation">,</span></span>
<span class="line">    <span class="token punctuation">]</span><span class="token punctuation">)</span></span>
<span class="line">    <span class="token operator">-></span><span class="token function">search</span><span class="token punctuation">(</span><span class="token variable">$connection</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><blockquote>
<p>Note: if you specify a custom select for a facet, ensure the facet name has the corresponding column inside the select statement. For example, if you have specified a facet named ‘my_facet’, its select statement should contain ‘my_facet’ attribute or an expression aliased as ‘my_facet’ (‘expr() AS my_facet’).</p>
</blockquote>
<h2 id="使用-data-providers" tabindex="-1"><a class="header-anchor" href="#使用-data-providers"><span>使用 data providers</span></a></h2>
<p>You can use [[\yii\data\ActiveDataProvider]] with the [[\yii\sphinx\Query]] and [[\yii\sphinx\ActiveQuery]]:</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token keyword">use</span> <span class="token package">yii<span class="token punctuation">\</span>data<span class="token punctuation">\</span>ActiveDataProvider</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token keyword">use</span> <span class="token package">yii<span class="token punctuation">\</span>sphinx<span class="token punctuation">\</span>Query</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token variable">$query</span> <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">Query</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token variable">$query</span><span class="token operator">-></span><span class="token function">from</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'yii2_test_article_index'</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">match</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'development'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token variable">$provider</span> <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">ActiveDataProvider</span><span class="token punctuation">(</span><span class="token punctuation">[</span></span>
<span class="line">    <span class="token string single-quoted-string">'query'</span> <span class="token operator">=></span> <span class="token variable">$query</span><span class="token punctuation">,</span></span>
<span class="line">    <span class="token string single-quoted-string">'pagination'</span> <span class="token operator">=></span> <span class="token punctuation">[</span></span>
<span class="line">        <span class="token string single-quoted-string">'pageSize'</span> <span class="token operator">=></span> <span class="token number">10</span><span class="token punctuation">,</span></span>
<span class="line">    <span class="token punctuation">]</span></span>
<span class="line"><span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token variable">$models</span> <span class="token operator">=</span> <span class="token variable">$provider</span><span class="token operator">-></span><span class="token function">getModels</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token keyword">use</span> <span class="token package">yii<span class="token punctuation">\</span>data<span class="token punctuation">\</span>ActiveDataProvider</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token keyword">use</span> <span class="token package">app<span class="token punctuation">\</span>models<span class="token punctuation">\</span>Article</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token variable">$provider</span> <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">ActiveDataProvider</span><span class="token punctuation">(</span><span class="token punctuation">[</span></span>
<span class="line">    <span class="token string single-quoted-string">'query'</span> <span class="token operator">=></span> <span class="token class-name static-context">Article</span><span class="token operator">::</span><span class="token function">find</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">,</span></span>
<span class="line">    <span class="token string single-quoted-string">'pagination'</span> <span class="token operator">=></span> <span class="token punctuation">[</span></span>
<span class="line">        <span class="token string single-quoted-string">'pageSize'</span> <span class="token operator">=></span> <span class="token number">10</span><span class="token punctuation">,</span></span>
<span class="line">    <span class="token punctuation">]</span></span>
<span class="line"><span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token variable">$models</span> <span class="token operator">=</span> <span class="token variable">$provider</span><span class="token operator">-></span><span class="token function">getModels</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>However, if you want to use <a href="https://github.com/yiisoft/yii2-sphinx/blob/master/docs/guide/usage-facets.md" target="_blank" rel="noopener noreferrer">‘facet’ feature</a> or <a href="https://github.com/yiisoft/yii2-sphinx/blob/master/docs/guide/usage-meta.md" target="_blank" rel="noopener noreferrer">query meta information</a> benefit you need to use <code v-pre>yii\sphinx\ActiveDataProvider</code>. It allows preparing total item count using query ‘meta’ information and fetching of the facet results:</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token keyword">use</span> <span class="token package">yii<span class="token punctuation">\</span>sphinx<span class="token punctuation">\</span>ActiveDataProvider</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token keyword">use</span> <span class="token package">yii<span class="token punctuation">\</span>sphinx<span class="token punctuation">\</span>Query</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token variable">$query</span> <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">Query</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token variable">$query</span><span class="token operator">-></span><span class="token function">from</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'idx_item'</span><span class="token punctuation">)</span></span>
<span class="line">    <span class="token operator">-></span><span class="token function">match</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'foo'</span><span class="token punctuation">)</span></span>
<span class="line">    <span class="token operator">-></span><span class="token function">showMeta</span><span class="token punctuation">(</span><span class="token constant boolean">true</span><span class="token punctuation">)</span></span>
<span class="line">    <span class="token operator">-></span><span class="token function">facets</span><span class="token punctuation">(</span><span class="token punctuation">[</span></span>
<span class="line">        <span class="token string single-quoted-string">'brand_id'</span><span class="token punctuation">,</span></span>
<span class="line">        <span class="token string single-quoted-string">'categories'</span><span class="token punctuation">,</span></span>
<span class="line">    <span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token variable">$provider</span> <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">ActiveDataProvider</span><span class="token punctuation">(</span><span class="token punctuation">[</span></span>
<span class="line">    <span class="token string single-quoted-string">'query'</span> <span class="token operator">=></span> <span class="token variable">$query</span><span class="token punctuation">,</span></span>
<span class="line">    <span class="token string single-quoted-string">'pagination'</span> <span class="token operator">=></span> <span class="token punctuation">[</span></span>
<span class="line">        <span class="token string single-quoted-string">'pageSize'</span> <span class="token operator">=></span> <span class="token number">10</span><span class="token punctuation">,</span></span>
<span class="line">    <span class="token punctuation">]</span></span>
<span class="line"><span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token variable">$models</span> <span class="token operator">=</span> <span class="token variable">$provider</span><span class="token operator">-></span><span class="token function">getModels</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token variable">$facets</span> <span class="token operator">=</span> <span class="token variable">$provider</span><span class="token operator">-></span><span class="token function">getFacets</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token variable">$brandIdFacet</span> <span class="token operator">=</span> <span class="token variable">$provider</span><span class="token operator">-></span><span class="token function">getFacet</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'brand_id'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><blockquote>
<p>Note: Because pagination offset and limit may exceed Sphinx ‘max_matches’ bounds, data provider will set ‘max_matches’ option automatically based on those values. However, if [[Query::showMeta]] is set, such adjustment is not performed as it will break total count calculation, so you’ll have to deal with ‘max_matches’ bounds on your own.</p>
</blockquote>
<h2 id="构造-snippets-节选" tabindex="-1"><a class="header-anchor" href="#构造-snippets-节选"><span>构造 Snippets (节选)</span></a></h2>
<p>Snippet (excerpt) - is a fragment of the index source text, which contains highlighted words from fulltext search condition. Sphinx has a powerful build-in mechanism to compose snippets. However, since Sphinx does not store the original indexed text, the snippets for the rows in query result should be build separately via another query. Such query may be performed via <code v-pre>yii\sphinx\Command::callSnippets()</code>:</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token variable">$sql</span> <span class="token operator">=</span> <span class="token string double-quoted-string">"SELECT * FROM idx_item WHERE MATCH('about')"</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token variable">$rows</span> <span class="token operator">=</span> <span class="token class-name static-context">Yii</span><span class="token operator">::</span><span class="token variable">$app</span><span class="token operator">-></span><span class="token property">sphinx</span><span class="token operator">-></span><span class="token function">createCommand</span><span class="token punctuation">(</span><span class="token variable">$sql</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">queryAll</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token variable">$rowSnippetSources</span> <span class="token operator">=</span> <span class="token punctuation">[</span><span class="token punctuation">]</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token keyword">foreach</span> <span class="token punctuation">(</span><span class="token variable">$rows</span> <span class="token keyword">as</span> <span class="token variable">$row</span><span class="token punctuation">)</span> <span class="token punctuation">{</span></span>
<span class="line">    <span class="token variable">$rowSnippetSources</span><span class="token punctuation">[</span><span class="token punctuation">]</span> <span class="token operator">=</span> <span class="token function">file_get_contents</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'/path/to/index/files/'</span> <span class="token operator">.</span> <span class="token variable">$row</span><span class="token punctuation">[</span><span class="token string single-quoted-string">'id'</span><span class="token punctuation">]</span> <span class="token operator">.</span> <span class="token string single-quoted-string">'.txt'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token punctuation">}</span></span>
<span class="line"></span>
<span class="line"><span class="token variable">$snippets</span> <span class="token operator">=</span> <span class="token class-name static-context">Yii</span><span class="token operator">::</span><span class="token variable">$app</span><span class="token operator">-></span><span class="token property">sphinx</span><span class="token operator">-></span><span class="token function">createCommand</span><span class="token punctuation">(</span><span class="token variable">$sql</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">callSnippets</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'idx_item'</span><span class="token punctuation">,</span> <span class="token variable">$rowSnippetSources</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'about'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>You can simplify this workflow using [[yii\sphinx\Query::snippetCallback]]. It is a PHP callback, which receives array of query result rows as an argument and must return the array of snippet source strings in the order, which match one of incoming rows. Example:</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token keyword">use</span> <span class="token package">yii<span class="token punctuation">\</span>sphinx<span class="token punctuation">\</span>Query</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token variable">$query</span> <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">Query</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token variable">$rows</span> <span class="token operator">=</span> <span class="token variable">$query</span><span class="token operator">-></span><span class="token function">from</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'idx_item'</span><span class="token punctuation">)</span></span>
<span class="line">    <span class="token operator">-></span><span class="token function">match</span><span class="token punctuation">(</span><span class="token variable">$_POST</span><span class="token punctuation">[</span><span class="token string single-quoted-string">'search'</span><span class="token punctuation">]</span><span class="token punctuation">)</span></span>
<span class="line">    <span class="token operator">-></span><span class="token function">snippetCallback</span><span class="token punctuation">(</span><span class="token keyword">function</span> <span class="token punctuation">(</span><span class="token variable">$rows</span><span class="token punctuation">)</span> <span class="token punctuation">{</span></span>
<span class="line">        <span class="token variable">$result</span> <span class="token operator">=</span> <span class="token punctuation">[</span><span class="token punctuation">]</span><span class="token punctuation">;</span></span>
<span class="line">        <span class="token keyword">foreach</span> <span class="token punctuation">(</span><span class="token variable">$rows</span> <span class="token keyword">as</span> <span class="token variable">$row</span><span class="token punctuation">)</span> <span class="token punctuation">{</span></span>
<span class="line">            <span class="token variable">$result</span><span class="token punctuation">[</span><span class="token punctuation">]</span> <span class="token operator">=</span> <span class="token function">file_get_contents</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'/path/to/index/files/'</span> <span class="token operator">.</span> <span class="token variable">$row</span><span class="token punctuation">[</span><span class="token string single-quoted-string">'id'</span><span class="token punctuation">]</span> <span class="token operator">.</span> <span class="token string single-quoted-string">'.txt'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line">        <span class="token punctuation">}</span></span>
<span class="line">        <span class="token keyword">return</span> <span class="token variable">$result</span><span class="token punctuation">;</span></span>
<span class="line">    <span class="token punctuation">}</span><span class="token punctuation">)</span></span>
<span class="line">    <span class="token operator">-></span><span class="token function">all</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token keyword">foreach</span> <span class="token punctuation">(</span><span class="token variable">$rows</span> <span class="token keyword">as</span> <span class="token variable">$row</span><span class="token punctuation">)</span> <span class="token punctuation">{</span></span>
<span class="line">    <span class="token keyword">echo</span> <span class="token variable">$row</span><span class="token punctuation">[</span><span class="token string single-quoted-string">'snippet'</span><span class="token punctuation">]</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token punctuation">}</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>If you are using Active Record, you can [[yii\sphinx\ActiveQuery::snippetByModel()]] to compose a snippets. This method retrieves snippet source per each row calling <code v-pre>getSnippetSource()</code> method of the result model. All you need to do is implement it in your Active Record class, so it return the correct value:</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token keyword">use</span> <span class="token package">yii<span class="token punctuation">\</span>sphinx<span class="token punctuation">\</span>ActiveRecord</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token keyword">class</span> <span class="token class-name-definition class-name">Article</span> <span class="token keyword">extends</span> <span class="token class-name">ActiveRecord</span></span>
<span class="line"><span class="token punctuation">{</span></span>
<span class="line">    <span class="token keyword">public</span> <span class="token keyword">function</span> <span class="token function-definition function">getSnippetSource</span><span class="token punctuation">(</span><span class="token punctuation">)</span></span>
<span class="line">    <span class="token punctuation">{</span></span>
<span class="line">        <span class="token keyword">return</span> <span class="token function">file_get_contents</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'/path/to/source/files/'</span> <span class="token operator">.</span> <span class="token variable">$this</span><span class="token operator">-></span><span class="token property">id</span> <span class="token operator">.</span> <span class="token string single-quoted-string">'.txt'</span><span class="token punctuation">)</span><span class="token punctuation">;</span><span class="token punctuation">;</span></span>
<span class="line">    <span class="token punctuation">}</span></span>
<span class="line"><span class="token punctuation">}</span></span>
<span class="line"></span>
<span class="line"><span class="token variable">$articles</span> <span class="token operator">=</span> <span class="token class-name static-context">Article</span><span class="token operator">::</span><span class="token function">find</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">snippetByModel</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">all</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token keyword">foreach</span> <span class="token punctuation">(</span><span class="token variable">$articles</span> <span class="token keyword">as</span> <span class="token variable">$article</span><span class="token punctuation">)</span> <span class="token punctuation">{</span></span>
<span class="line">    <span class="token keyword">echo</span> <span class="token variable">$article</span><span class="token operator">-></span><span class="token property">snippet</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token punctuation">}</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h2 id="使用-gii-生成器" tabindex="-1"><a class="header-anchor" href="#使用-gii-生成器"><span>使用 Gii 生成器</span></a></h2>
<p>This extension provides a code generator, which can be integrated with yii ‘gii’ module. It allows generation of the Active Record code. In order to enable it, you should adjust your application configuration in following way:</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token keyword">return</span> <span class="token punctuation">[</span></span>
<span class="line">    <span class="token comment">//....</span></span>
<span class="line">    <span class="token string single-quoted-string">'modules'</span> <span class="token operator">=></span> <span class="token punctuation">[</span></span>
<span class="line">        <span class="token comment">// ...</span></span>
<span class="line">        <span class="token string single-quoted-string">'gii'</span> <span class="token operator">=></span> <span class="token punctuation">[</span></span>
<span class="line">            <span class="token string single-quoted-string">'class'</span> <span class="token operator">=></span> <span class="token string single-quoted-string">'yii\gii\Module'</span><span class="token punctuation">,</span></span>
<span class="line">            <span class="token string single-quoted-string">'generators'</span> <span class="token operator">=></span> <span class="token punctuation">[</span></span>
<span class="line">                <span class="token string single-quoted-string">'sphinxModel'</span> <span class="token operator">=></span> <span class="token punctuation">[</span></span>
<span class="line">                    <span class="token string single-quoted-string">'class'</span> <span class="token operator">=></span> <span class="token string single-quoted-string">'yii\sphinx\gii\model\Generator'</span></span>
<span class="line">                <span class="token punctuation">]</span></span>
<span class="line">            <span class="token punctuation">]</span><span class="token punctuation">,</span></span>
<span class="line">        <span class="token punctuation">]</span><span class="token punctuation">,</span></span>
<span class="line">    <span class="token punctuation">]</span></span>
<span class="line"><span class="token punctuation">]</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h2 id="浮点型参数绑定" tabindex="-1"><a class="header-anchor" href="#浮点型参数绑定"><span>浮点型参数绑定</span></a></h2>
<p>There are issue related to float values binding using PDO and SphinxQL. PDO does not provide a way to bind a float parameter in prepared statement mode, thus float values are passed with mode <code v-pre>PDO::PARAM_STR</code> and thus are bound to the statement as quoted strings, e.g. <code v-pre>'9.85'</code>. Unfortunally, SphinxQL is unable to recognize float values passed in this way, producing following error:</p>
<blockquote>
<p>syntax error, unexpected QUOTED_STRING, expecting CONST_INT or CONST_FLOAT</p>
</blockquote>
<p>In order to bypass this problem any parameter bind to the [[\yii\sphinx\Command]], which PHP type is exactly ‘float’, will be inserted to the SphinxQL content as literal instead of being bound.</p>
<p>This feature works only if value is a native PHP float (strings containing floats do not work). For example:</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token keyword">use</span> <span class="token package">yii<span class="token punctuation">\</span>sphinx<span class="token punctuation">\</span>Query</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token comment">// following code works fine:</span></span>
<span class="line"><span class="token variable">$rows</span> <span class="token operator">=</span> <span class="token punctuation">(</span><span class="token keyword">new</span> <span class="token class-name">Query</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">from</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'item_index'</span><span class="token punctuation">)</span></span>
<span class="line">    <span class="token operator">-></span><span class="token function">where</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'price > :price AND price &lt; :priceMax'</span><span class="token punctuation">,</span> <span class="token punctuation">[</span></span>
<span class="line">        <span class="token string single-quoted-string">'price'</span> <span class="token operator">=></span> <span class="token number">2.1</span><span class="token punctuation">,</span></span>
<span class="line">        <span class="token string single-quoted-string">'priceMax'</span> <span class="token operator">=></span> <span class="token number">2.9</span><span class="token punctuation">,</span></span>
<span class="line">    <span class="token punctuation">]</span><span class="token punctuation">)</span></span>
<span class="line">    <span class="token operator">-></span><span class="token function">all</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token comment">// this one produces an error:</span></span>
<span class="line"><span class="token variable">$rows</span> <span class="token operator">=</span> <span class="token punctuation">(</span><span class="token keyword">new</span> <span class="token class-name">Query</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">from</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'item_index'</span><span class="token punctuation">)</span></span>
<span class="line">    <span class="token operator">-></span><span class="token function">where</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'price > :price AND price &lt; :priceMax'</span><span class="token punctuation">,</span> <span class="token punctuation">[</span></span>
<span class="line">        <span class="token string single-quoted-string">'price'</span> <span class="token operator">=></span> <span class="token string single-quoted-string">'2.1'</span><span class="token punctuation">,</span></span>
<span class="line">        <span class="token string single-quoted-string">'priceMax'</span> <span class="token operator">=></span> <span class="token string single-quoted-string">'2.9'</span><span class="token punctuation">,</span></span>
<span class="line">    <span class="token punctuation">]</span><span class="token punctuation">)</span></span>
<span class="line">    <span class="token operator">-></span><span class="token function">all</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>However, in case you are using ‘hash’ conditions over the index fields declared as ‘float’, the type conversion will be performed automatically:</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token keyword">use</span> <span class="token package">yii<span class="token punctuation">\</span>sphinx<span class="token punctuation">\</span>Query</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token comment">// following code works fine in case 'price' is a float field in 'item_index':</span></span>
<span class="line"><span class="token variable">$rows</span> <span class="token operator">=</span> <span class="token punctuation">(</span><span class="token keyword">new</span> <span class="token class-name">Query</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">from</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'item_index'</span><span class="token punctuation">)</span></span>
<span class="line">    <span class="token operator">-></span><span class="token function">where</span><span class="token punctuation">(</span><span class="token punctuation">[</span></span>
<span class="line">        <span class="token string single-quoted-string">'price'</span> <span class="token operator">=></span> <span class="token string single-quoted-string">'2.5'</span></span>
<span class="line">    <span class="token punctuation">]</span><span class="token punctuation">)</span></span>
<span class="line">    <span class="token operator">-></span><span class="token function">all</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><blockquote>
<p>Note: it could be by the time you are reading this float param binding is already fixed at Sphinx server side, or there are other concerns about this functionality, which make it undesirable. In this case you can disable automatic float params conversion via [[\yii\sphinx\Connection::enableFloatConversion]].</p>
</blockquote>
<h2 id="使用分布式索引" tabindex="-1"><a class="header-anchor" href="#使用分布式索引"><span>使用分布式索引</span></a></h2>
<p>This extension uses <code v-pre>DESCRIBE</code> query in order to fetch information about Sphinx index structure (field names and types). However for the <a href="http://sphinxsearch.com/docs/current.html#distributed" target="_blank" rel="noopener noreferrer">distributed indexes</a> it is not always possible. Schema of such index can be found only, if its declaration contains at list one available local index. For example:</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line">index item_distributed</span>
<span class="line"><span class="token punctuation">{</span></span>
<span class="line">    type <span class="token operator">=</span> distributed</span>
<span class="line"></span>
<span class="line">    <span class="token comment"># local index :</span></span>
<span class="line">    local <span class="token operator">=</span> item_local</span>
<span class="line"></span>
<span class="line">    <span class="token comment"># remote indexes :</span></span>
<span class="line">    agent <span class="token operator">=</span> <span class="token number">127.0</span><span class="token number">.0</span><span class="token number">.1</span><span class="token punctuation">:</span><span class="token number">9312</span><span class="token punctuation">:</span>remote_item_1</span>
<span class="line">    agent <span class="token operator">=</span> <span class="token number">127.0</span><span class="token number">.0</span><span class="token number">.1</span><span class="token punctuation">:</span><span class="token number">9313</span><span class="token punctuation">:</span>remote_item_2</span>
<span class="line">    <span class="token comment"># ...</span></span>
<span class="line"><span class="token punctuation">}</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>It is recommended to have at least one local index in the distributed index declaration. You are not forced to actually use it - this local index may be empty, it is needed for the schema declaration only.</p>
<p>Still it is allowed to specify distributed index without local one. For such index the default dummy schema will be used. However in this case automatic typecasting for the index fields will be unavailable and you should perform data typecast on your own. For example:</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token keyword">use</span> <span class="token package">yii<span class="token punctuation">\</span>sphinx<span class="token punctuation">\</span>Query</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token comment">// distributed index with local</span></span>
<span class="line"><span class="token variable">$rows</span> <span class="token operator">=</span> <span class="token punctuation">(</span><span class="token keyword">new</span> <span class="token class-name">Query</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">from</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'item_distributed_with_local'</span><span class="token punctuation">)</span></span>
<span class="line">    <span class="token operator">-></span><span class="token function">where</span><span class="token punctuation">(</span><span class="token punctuation">[</span><span class="token string single-quoted-string">'category_id'</span> <span class="token operator">=></span> <span class="token string single-quoted-string">'12'</span><span class="token punctuation">]</span><span class="token punctuation">)</span> <span class="token comment">// works fine string `'12'` - converted to integer `12`</span></span>
<span class="line">    <span class="token operator">-></span><span class="token function">all</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token comment">// distributed index without local</span></span>
<span class="line"><span class="token variable">$rows</span> <span class="token operator">=</span> <span class="token punctuation">(</span><span class="token keyword">new</span> <span class="token class-name">Query</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">from</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'item_distributed_without_local'</span><span class="token punctuation">)</span></span>
<span class="line">    <span class="token operator">-></span><span class="token function">where</span><span class="token punctuation">(</span><span class="token punctuation">[</span><span class="token string single-quoted-string">'category_id'</span> <span class="token operator">=></span> <span class="token string single-quoted-string">'12'</span><span class="token punctuation">]</span><span class="token punctuation">)</span> <span class="token comment">// produces SphinxQL error: 'syntax error, unexpected QUOTED_STRING, expecting CONST_INT'</span></span>
<span class="line">    <span class="token operator">-></span><span class="token function">all</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token variable">$rows</span> <span class="token operator">=</span> <span class="token punctuation">(</span><span class="token keyword">new</span> <span class="token class-name">Query</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">from</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'item_distributed_without_local'</span><span class="token punctuation">)</span></span>
<span class="line">    <span class="token operator">-></span><span class="token function">where</span><span class="token punctuation">(</span><span class="token punctuation">[</span><span class="token string single-quoted-string">'category_id'</span> <span class="token operator">=></span> <span class="token punctuation">(</span><span class="token keyword type-casting">int</span><span class="token punctuation">)</span><span class="token string single-quoted-string">'12'</span><span class="token punctuation">]</span><span class="token punctuation">)</span> <span class="token comment">// need to perform typecasting</span></span>
<span class="line">    <span class="token operator">-></span><span class="token function">all</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><blockquote>
<p>💖喜欢本文档的，欢迎点赞、收藏、留言或转发，谢谢支持！<br>
作者邮箱：zhuzixian520@126.com，github地址：<a href="https://github.com/zhuzixian520" target="_blank" rel="noopener noreferrer">github.com/zhuzixian520</a></p>
</blockquote>
</div></template>


